﻿using System;
using System.Collections.Generic;
using System.Windows.Forms;
using Data.Common;
using Data.Enum;
using Data.Model;
using DataAccess.BikeManage;
using DataAccess.Common;
using Ninject;
using WinForm.Helper;

namespace WinForm.Dialog
{
    public partial class EditControlBox : Form
    {
        private static readonly IKernel Kernel = new StandardKernel(new DbModule());
        private readonly IControlBox _controlBox = Kernel.Get<IControlBox>();
        private readonly ILockArea _lockArea = Kernel.Get<ILockArea>();
        private readonly ILookUpDataService _lookUpDataService = Kernel.Get<ILookUpDataService>();

        public EditControlBox()
        {
            InitializeComponent();
        }

        public EditControlBox(int id)
        {
            InitializeComponent();
            if (id > 0)
            {
                lbControlBoxid.Text = id.ToString();
                ResultInfo<jsh_controlbox> controlBox = _controlBox.GetControlBoxById(id);
                txtAddress.Text = controlBox.Obj.Address;
                txtBikecount.Text = controlBox.Obj.Bikenum.ToString();
                txtBoxno.Text = controlBox.Obj.Name;
                txtIpAddress.Text = controlBox.Obj.Ip;
                txtPileCount.Text = controlBox.Obj.Pilenum.ToString();
            }
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
                if (String.IsNullOrEmpty(txtBoxno.Text) || string.IsNullOrWhiteSpace(txtBoxno.Text))
                {
                    MyMessageBox.ShowMessageBoxInfo("请输入控制箱编号！");
                    return;
                }

                if (String.IsNullOrEmpty(txtPileCount.Text) || string.IsNullOrWhiteSpace(txtPileCount.Text))
                {
                    MyMessageBox.ShowMessageBoxInfo("请输入锁柱数目！");
                    return;
                }

                var controlbox = new jsh_controlbox
                                     {
                                         id = Converts.ConvertToInt(lbControlBoxid.Text),
                                         Name = txtBoxno.Text,
                                         Bikenum = Converts.ConvertToInt(txtBikecount.Text),
                                         Pilenum = Converts.ConvertToInt(txtPileCount.Text),
                                         Status =
                                             cmbStatus.SelectedValue == null ? "" : cmbStatus.SelectedValue.ToString(),
                                         Address = txtAddress.Text,
                                         Ip = txtIpAddress.Text,
                                         Ver = cmbVersion.Text,
                                         Areaid =
                                             cmbLockAddress.SelectedValue == null
                                                 ? 0
                                                 : Converts.ConvertToInt(cmbLockAddress.SelectedValue.ToString()),
                                         Area = cmbLockAddress.Text
                                     };
                ResultInfo<int> id = _controlBox.CreateOrUpdateControlBox(controlbox);
                if (id.ResultNumber == ResultEnum.Success && id.Obj > 0)
                {
                    if (!string.IsNullOrEmpty(lbControlBoxid.Text))
                    {
                        MyMessageBox.ShowMessageBoxInfo("修改控制箱信息成功！");
                    }
                    if (string.IsNullOrEmpty(lbControlBoxid.Text))
                    {
                        MyMessageBox.ShowMessageBoxInfo("新增控制箱信息成功！");
                    }
                    DialogResult = DialogResult.OK;
                }
                else
                {
                    MyMessageBox.ShowMessageBoxInfo(id.Describe);
                }
            }
            catch (Exception)
            {
                MyMessageBox.ShowMessageBoxInfo("保存失败！");
            }
        }

        private void EditControlBox_Load(object sender, EventArgs e)
        {
            try
            {
                InitializeAreas();

                InitializeStatus();
                InitializeVersion();
            }
            catch (Exception ex)
            {
                MyMessageBox.ShowMessageBoxError(ex.Message);
            }
        }

        private void InitializeVersion()
        {
            cmbVersion.SelectedIndex = 0;
        }

        private void InitializeStatus()
        {
            List<IdNameObject> status = _lookUpDataService.GetLockaddressStatus();

            cmbStatus.DataSource = status;
            cmbStatus.DisplayMember = "Name";
            cmbStatus.ValueMember = "Name";
            cmbStatus.SelectedIndex = 0;
        }

        private void InitializeAreas()
        {
            ResultInfo<IList<jsh_Area>> areas = _lockArea.GetLockArea(0);
            if (areas.ResultNumber == ResultEnum.Success && areas.Obj.Count > 0)
            {
                cmbLockAddress.DataSource = areas.Obj;
                cmbLockAddress.DisplayMember = "Area";
                cmbLockAddress.ValueMember = "id";
                cmbLockAddress.SelectedIndex = 0;
            }
        }

        private void btnControlCancel_Click(object sender, EventArgs e)
        {
            Close();
            Dispose();
        }
    }
}